package com.kanshu.ksgb.fastread.doudou.module.makemoney.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.base.basemvp.IGenrialMvpView;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.SwipeRefreshHelper;
import com.kanshu.ksgb.fastread.doudou.common.view.LoadingDialog;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.event.BindEvent;
import com.kanshu.ksgb.fastread.doudou.common.business.event.LoginEvent;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.bean.WeeklyBean;
import com.kanshu.ksgb.fastread.doudou.common.business.event.TaskEvent;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.presenter.MakeMoneyPresenter;
import com.kanshu.ksgb.fastread.doudou.module.makemoney.retrofit.ReceiveParams;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.BindInfo;
import com.kanshu.ksgb.fastread.doudou.module.personal.bean.UpdateInfoEvent;
import com.kanshu.ksgb.fastread.doudou.module.signin.event.SignInEvent;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by wcy on 18/5/28
 */

@SuppressLint("SetTextI18n")
public class WeeklyFragment extends BaseFragment implements IGenrialMvpView<BaseResult<WeeklyBean>> {
    @BindView(R.id.btn_1)
    SuperTextView btn1;
    @BindView(R.id.btn_2)
    SuperTextView btn2;
    @BindView(R.id.btn_3)
    SuperTextView btn3;
    @BindView(R.id.btn_4)
    SuperTextView btn4;

    @BindView(R.id.progress1)
    ProgressBar progressBar1;
    @BindView(R.id.progress2)
    ProgressBar progressBar2;
    @BindView(R.id.progress3)
    ProgressBar progressBar3;
    @BindView(R.id.progress4)
    ProgressBar progressBar4;

    @BindView(R.id.label2)
    TextView m2CoinBean;
    @BindView(R.id.label3)
    TextView mContinueSignProgress;
    @BindView(R.id.label5)
    TextView m350MinLabel;
    @BindView(R.id.label6)
    TextView m5CoinBean;
    @BindView(R.id.label7)
    TextView m350MinProgress;
    @BindView(R.id.label9)
    TextView m700MinLabel;
    @BindView(R.id.label10)
    TextView m10CoinBean;
    @BindView(R.id.label11)
    TextView m700MinProgress;
    @BindView(R.id.label13)
    TextView m1100MinLabel;
    @BindView(R.id.label14)
    TextView m20CoinBean;
    @BindView(R.id.label15)
    TextView m1100MinProgress;
    Unbinder unbinder;
    MakeMoneyPresenter mPresenter;
    WeeklyBean mWeeklyBean;
    @BindView(R.id.label8)
    TextView label8;
    @BindView(R.id.swipe_refresh)
    TwinklingRefreshLayout mSwipeRefresh;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_weekly_make_money_layout, null);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        init();
        return view;
    }

    private void init() {
        mPresenter = new MakeMoneyPresenter(lifeCyclerSubject);
        mPresenter.setWeeklyView(this);
        SwipeRefreshHelper.init(mSwipeRefresh, () -> {
            mPresenter.getWeeklyInfo();
        });
        mPresenter.getWeeklyInfo();

    }

    @OnClick({R.id.btn_1,R.id.btn_2,R.id.btn_3,R.id.btn_4})
    public void onClick(View v) {
        ReceiveParams params = new ReceiveParams();
        switch (v.getId()) {
            case R.id.btn_1:
                break;
            case R.id.btn_3:
                if (mWeeklyBean != null && mWeeklyBean.read_700 >= 700
                        && mWeeklyBean.receive_time_700 <= 0) {
                    params.receive_type = ReceiveParams.RECEIVE_TIME_700;
                    showLoading("");
                    mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                        @Override
                        public void onResponse(Boolean aBoolean) {
                            m700MinProgress.setText("已领取");
                            jobFinish(btn3,2,progressBar3,m700MinProgress);
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }

                        @Override
                        public void onError(int code, String errDesc) {
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }
                    });
                }
                break;
            case R.id.btn_2:
                if (mWeeklyBean != null && mWeeklyBean.read_350 >= 350
                        && mWeeklyBean.receive_time_350 <= 0) {
                    params.receive_type = ReceiveParams.RECEIVE_TIME_350;
                    showLoading("");
                    mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                        @Override
                        public void onResponse(Boolean aBoolean) {
                            m350MinProgress.setText("已领取");
                            jobFinish(btn2,2,progressBar2,m350MinProgress);
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }

                        @Override
                        public void onError(int code, String errDesc) {
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }
                    });
                }
                break;
            case R.id.btn_4:
                if (mWeeklyBean != null && mWeeklyBean.read_time_1100 >= 1100
                        && mWeeklyBean.receive_time_1100 <= 0) {
                    params.receive_type = ReceiveParams.RECEIVE_TIME_1100;
                    showLoading("");
                    mPresenter.receiveTask(params, new INetCommCallback<Boolean>() {
                        @Override
                        public void onResponse(Boolean aBoolean) {
                            m1100MinProgress.setText("已领取");
                            jobFinish(btn4,2,progressBar4,m1100MinProgress);
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }

                        @Override
                        public void onError(int code, String errDesc) {
                            dismissLoading();
                            EventBus.getDefault().post(new UpdateInfoEvent());
                        }
                    });
                }
                break;
        }
    }

    @Override
    protected void updateViews() {
        super.updateViews();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        mPresenter.detachView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleTaskEvent(TaskEvent event) {
        mPresenter.getWeeklyInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleBindEvent(BindEvent event) {
        if (event.code == BindEvent.SUCCESS) {
            mPresenter.getWeeklyInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleBindInfo(BindInfo event) {
        if (event.result) {
            mPresenter.getWeeklyInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleSignInEvent(SignInEvent event) {
        if (event.status == SignInEvent.STATUS_SUCCESS) {
            mPresenter.getWeeklyInfo();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleLoginEvent(LoginEvent event) {
        mPresenter.getWeeklyInfo();
    }

    LoadingDialog loadingDialog;
    public void showLoading(String msg){
        if(loadingDialog == null){
            loadingDialog = new LoadingDialog(getContext(),msg);
        }
        if(!loadingDialog.isShowing()){
            loadingDialog.show();
        }
    }
    public void dismissLoading(){
        if(loadingDialog !=null && loadingDialog.isShowing()){
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showContent(BaseResult<WeeklyBean> result) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
        WeeklyBean data = result.result.data;
        if (data == null) {
            return;
        }
        mWeeklyBean = data;
        int color1 = Color.parseColor("#ff232b71");
        if (data.sign_in_info > 0) {
            mContinueSignProgress.setText("进度 " + data.sign_in_info + "/7");
            progressBar1.setMax(7);
            progressBar1.setProgress(data.sign_in_info);
        }else{
            mContinueSignProgress.setText("领取奖励");
        }

        if (data.receive_time_350 > 0) {
            jobFinish(btn2,2,progressBar2,m350MinProgress);
        } else if (data.read_350 >= 350) {
            jobFinish(btn2,1,progressBar2,m350MinProgress);
        } else {
            m350MinProgress.setText("进度 " + BookUtils.getPercentStr(data.read_350, 350));
            m350MinProgress.setTextColor(color1);
            progressBar2.setProgress( data.read_350 * 100 / 350 );
        }

        if (data.receive_time_700 > 0) {
            jobFinish(btn3,2,progressBar3,m700MinProgress);
        } else if (data.read_700 >= 700) {
            jobFinish(btn3,1,progressBar3,m700MinProgress);
        } else {
            progressBar3.setProgress( data.read_700 * 100 / 700 );
            m700MinProgress.setText("进度 " + BookUtils.getPercentStr(data.read_700, 700));
            m700MinProgress.setTextColor(color1);
        }

        if (data.receive_time_1100 > 0) {
            jobFinish(btn4,2,progressBar4,m1100MinProgress);
        } else if (data.read_1100 >= 1100) {
            jobFinish(btn4,1,progressBar4,m1100MinProgress);
        } else {
            progressBar4.setProgress( data.read_1100 * 100 / 1100 );
            m1100MinProgress.setText("进度 " + BookUtils.getPercentStr(data.read_1100, 1100));
            m1100MinProgress.setTextColor(color1);
        }
    }

    private void jobFinish(SuperTextView textView,int progress,ProgressBar progressBar, TextView tvProgress){
        int color0 = mContext.getResources().getColor(R.color.make_money_text_color0);
        int color1 = mContext.getResources().getColor(R.color.make_money_text_color1);
        int color2 = mContext.getResources().getColor(R.color.make_money_text_color2);
        int white= mContext.getResources().getColor(R.color.white);
        progressBar.setVisibility(View.GONE);
        textView.setVisibility(View.VISIBLE);
        tvProgress.setVisibility(View.GONE);
        switch (progress){
            case 1://已完成，未领取
                textView.setStrokeColor(white);
                textView.setText("领取奖励");
                textView.setSolid(color0);
                textView.setTextColor(color1);
                textView.setStrokeWidth(0);
                break;
            case 2://已领取
                textView.setSolid(white);
                textView.setText("已领取");
                textView.setStrokeColor(color2);
                textView.setTextColor(color2);
                textView.setStrokeWidth(1);
                break;
        }

    }

    @Override
    public void showError(int errCode, String errDesc) {
        SwipeRefreshHelper.swipeRefreshCompleted(mSwipeRefresh, null);
    }
}
